package com.esg.esgproject.mp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.esg.esgproject.mp.entity.auto.Company;
import com.esg.esgproject.mp.entity.auto.Country;
import com.esg.esgproject.mp.qo.QoCompany;
import com.esg.esgproject.mp.qo.QoCountry;
import com.esg.esgproject.mp.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author z100
 * @since 2021-12-15
 */
@RestController
@RequestMapping("/country")
public class CountryController {

    @Autowired
    CountryService countryService;

    /**
     * 增
     * @param country
     * @return boolean 是否保存成功
     */
    @PutMapping("/add")
    public ResponseEntity<Object> add(Country country){
        boolean res = countryService.save(country);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }
    
    /**
     *删
     * @param id
     * @return boolean 是否删除成功
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Object> delete(@RequestParam("id") Long id){
        boolean res = countryService.removeById(id);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

    /**
     *删
     * @param id
     * @return Country
     */

    @GetMapping("/getById")
    public ResponseEntity<Object> getById(@RequestParam("id")Integer id){
        LambdaQueryWrapper<Country> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(Country::getId,id);
        Country country = countryService.getOne(wrapper);
        return new ResponseEntity<>(country, HttpStatus.OK);
    }


    @GetMapping("/get")
    public ResponseEntity<Object> search(QoCountry qoCountry){
        LambdaQueryWrapper<Country> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(Country::getCountry,qoCountry.getCountry());
        List<Country> list = countryService.list(wrapper);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @GetMapping("/getAll")
    public ResponseEntity<Object> getAll(){
        List<Country> all = countryService.list();
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    @GetMapping("/update")
    public ResponseEntity<Object> update(Country country,Long oldId){
        LambdaQueryWrapper<Country> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Country::getId,oldId);
        boolean res = countryService.update(country, wrapper);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

}
